
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.PersonalizarJFrame;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfaceRoteador.java
 *
 * Created on 05/11/2011, 15:57:07
 */
/**
 *
 * @author Cleiton
 */
public class InterfaceRoteador extends javax.swing.JFrame {
//TODO fazer metodo para o Servidor do Roteador.

    private RoteadorImpl roteadorImpl;
    private PersonalizarJFrame personalizarJFrame;

    /** Creates new form InterfaceRoteador */
    public InterfaceRoteador() {
        try {
            roteadorImpl = new RoteadorImpl() {
                
                @Override
                void envieiPara(Pacote pacote, String envieiPara) {
                    jtfDestino.setText(pacote.getNameDestino());
                    jtfOrigem.setText(pacote.getNameOrigem());
                    jtfEnviar.setText(envieiPara);
                    jtfTtl.setText(Integer.toString(pacote.getNumerosDeRoteadores()));
                }
                
                @Override
                public void limparTelas() throws RemoteException {
                    jtfDestino.setText("");
                    jtfOrigem.setText("");
                    jtfEnviar.setText("");
                    jtfTtl.setText("");
                }
                
                @Override
                void alterarName(String name) {
                    alterarNome(name);
                }
            };
        } catch (RemoteException ex) {
            Logger.getLogger(InterfaceRoteador.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        roteadorImpl.conectarServidor();
        roteadorImpl.servidorCentarlAlteraNomeHost();
        instanciarRoteadorServidorRmi();
        personalizarJFrame = new PersonalizarJFrame(this);
        personalizarJFrame.setLookFeel();
        initComponents();
        this.setTitle("Roteador " + roteadorImpl.getName());
        personalizarJFrame.setPositionRElative();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtfDestino = new javax.swing.JTextField();
        jtfTtl = new javax.swing.JTextField();
        jtfOrigem = new javax.swing.JTextField();
        jtfEnviar = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Host Origem:");

        jLabel2.setText("Host Destino:");

        jLabel3.setText("Nº Roteadores:");

        jtfDestino.setEditable(false);

        jtfTtl.setEditable(false);
        jtfTtl.setToolTipText("Numero de Roteadores que este pacote já passou.");

        jtfOrigem.setEditable(false);

        jtfEnviar.setEditable(false);

        jLabel4.setText("Enviei para:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                        .addComponent(jtfOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jtfTtl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .addComponent(jtfDestino, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                            .addComponent(jtfEnviar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))))
                .addGap(109, 109, 109))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfOrigem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jtfDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfTtl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfEnviar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            
            public void run() {
                new InterfaceRoteador().setVisible(true);
            }
        });
    }

    /**
     *Inicializa o roteador para receber informações.
     */
    private void instanciarRoteadorServidorRmi() {
        try {            
            Naming.rebind("rmi://" + roteadorImpl.getIp() + "/" + roteadorImpl.getName(), roteadorImpl);
        } catch (RemoteException ex) {
            Logger.getLogger(InterfaceRoteador.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(InterfaceRoteador.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void alterarNome(String name) {
        this.setTitle("Roteador "+name);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jtfDestino;
    private javax.swing.JTextField jtfEnviar;
    private javax.swing.JTextField jtfOrigem;
    private javax.swing.JTextField jtfTtl;
    // End of variables declaration//GEN-END:variables
}
